package com.centling.smartSealForPhone.fragment

import android.content.Intent
import android.content.pm.PackageManager
import android.os.Bundle
import android.text.TextUtils
import android.view.View
import cn.jpush.android.api.JPushInterface
import com.centling.smartSealForPhone.R
import com.centling.smartSealForPhone.activity.*
import com.centling.smartSealForPhone.application.SmartSeal
import com.centling.smartSealForPhone.constants.SPConstants
import com.centling.smartSealForPhone.constants.UserType
import com.centling.smartSealForPhone.event.ReceiveUnreadMsgCountEvent
import com.centling.smartSealForPhone.receiver.TagAliasOperatorHelper
import com.centling.smartSealForPhone.utils.SPUtil
import com.centling.smartSealForPhone.utils.ShowDialog
import com.wsdk.service.BluetoothLeService
import kotlinx.android.synthetic.privateCloud.fragment_setting.*
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe

class SettingFragment : TitleBarFragment(), View.OnClickListener {

    override fun setLayoutResource() = R.layout.fragment_setting

    override fun findViewInThisFunction(rootView: View) {
        setTitleBarText("应用设置")
        setTitleBarVisibility(View.VISIBLE)

        EventBus.getDefault().register(this)

        initView()
        if (TextUtils.equals(SPUtil.getString(SPConstants.USER_TYPE), UserType.EQUIP_ADMIN)) {
            ll_set_report_bug.visibility = View.VISIBLE
        } else {
            ll_set_report_bug.visibility = View.GONE
        }
        if (TextUtils.equals(SPUtil.getString(SPConstants.USER_TYPE), UserType.SERVER_USER)) {
            ll_setting_alarm.visibility = View.VISIBLE
        } else {
            ll_setting_alarm.visibility = View.GONE
        }
    }

    fun initView() {
        ll_set_msg_info.setOnClickListener(this)
        ll_set_report_bug.setOnClickListener(this)
        ll_set_doc_layout.setOnClickListener(this)
        ll_set_feature_demo.setOnClickListener(this)
        ll_set_modify_pass.setOnClickListener(this)
        ll_setting_image_quality_adjust.setOnClickListener(this)
        ll_setting_alarm.setOnClickListener(this)
        ll_setting_app_version.setOnClickListener(this)
        setting_btn_logout.setOnClickListener(this)
        try {
            tv_setting_app_version.text = mContext.packageManager.getPackageInfo(SmartSeal.getInstance().packageName, 0).versionName
        } catch (e: PackageManager.NameNotFoundException) {
            e.printStackTrace()
        }
    }

    override fun onClick(view: View) {
        when (view.id) {
            R.id.ll_set_msg_info -> startActivity(Intent(mContext, MessageListActivity::class.java))
            R.id.ll_set_report_bug -> startActivity(Intent(mContext, BugReportActivity::class.java))
            R.id.ll_set_doc_layout -> startActivity(Intent(mContext, InstructionActivity::class.java))
            R.id.ll_set_feature_demo -> startActivity(Intent(mContext, FeatureDemoActivity::class.java))
            R.id.ll_set_modify_pass -> startActivity(Intent(mContext, ModifySelfPassActivity::class.java))
            R.id.ll_setting_app_version -> startActivity(Intent(mContext, AuditorAboutActivity::class.java))
            R.id.ll_setting_image_quality_adjust -> startActivity(Intent(mContext, ImageQualityActivity::class.java))
            R.id.ll_setting_alarm -> startActivity(Intent(mContext, AlarmListActivity::class.java))
            R.id.setting_btn_logout -> ShowDialog.showSelectDialog(mContext, "退出登录", "你确定要退出登录吗？", ""
            ) { tryToLogout() }
        }
    }

    private fun tryToLogout() {
        SPUtil.setBoolean(SPConstants.IS_SET_ALIAS, false)
        JPushInterface.cleanTags(SmartSeal.getInstance(), TagAliasOperatorHelper.sequence)
        val ip = SmartSeal.getInstance().ip
        val highQuality = SPUtil.getBoolean(SPConstants.HQ_UPLOAD)
        if (!SPUtil.getBoolean(SPConstants.REMEMBERUSER, false)) {
            SPUtil.clearAll()
        } else {
            val userName = SPUtil.getString(SPConstants.USER_NAME)
            val passWord = SPUtil.getString(SPConstants.USER_PSW)
            val isRemember = SPUtil.getBoolean(SPConstants.REMEMBERUSER)
            SPUtil.clearAll()
            SPUtil.setString(SPConstants.USER_NAME, userName)
            SPUtil.setString(SPConstants.USER_PSW, passWord)
            SPUtil.setBoolean(SPConstants.REMEMBERUSER, isRemember)
        }
        SmartSeal.getInstance().changeIp(ip)
        SPUtil.setBoolean(SPConstants.HQ_UPLOAD, highQuality)
        BluetoothLeService.disconnect()
        startActivity(Intent(mContext, LoginActivity::class.java))
        mActivity.finish()
    }

    override fun onDestroy() {
        super.onDestroy()
        EventBus.getDefault().unregister(this)
    }

    @Subscribe(sticky = true)
    fun receiveUnreadMsgCount(event: ReceiveUnreadMsgCountEvent) {
        if(event.count == 0) {
            tv_set_msg_info_unread_count?.visibility = View.GONE
        } else {
            tv_set_msg_info_unread_count?.visibility = View.VISIBLE
            tv_set_msg_info_unread_count?.text = "${event.count}"
        }
    }

    companion object {
        val instance: SettingFragment
            get() {
                val fragment = SettingFragment()
                val args = Bundle()
                fragment.arguments = args
                return fragment
            }
    }
}
